package juc.mythread.finish;

import lombok.extern.slf4j.Slf4j;
import juc.mythread.tools.SleepTools;


/**
 * ClassName TestJoin
 * @PackageName mythread.finish
 * @projectName myThread
 * @Description 
 * @Author games
 * @Date 2025/8/25 上午11:03
 * @Version 1.0
 */
@Slf4j
public class TestJoin {
    private static int count = 0;

    public void testJoin() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("开始执行");
            SleepTools.second(2);
            count = 5;
            log.debug("执行完成");
        }, "t1");
        t1.start();
        t1.join(500);//阻塞
        //t1.join();//阻塞
        log.debug("count: {}", count);
        log.debug("t1 isAlive: {}", t1.isAlive());
    }

    /*在JUnit环境中，测试方法执行完毕后，JVM可能不会等待后台线程完成
    在main方法中，主线程结束后JVM通常会等待非守护线程完成（除非显式退出）*/
    public static void main(String[] args) throws InterruptedException {
        System.out.println("开始执行main");

        Thread t1 = new Thread(() -> {
            System.out.println("开始执行");
            SleepTools.second(2);
            count = 5;
            System.out.println("执行完成");
        }, "t1");
        t1.start();

        //SleepTools.second(1);

        t1.join(500);

        System.out.println("结果为:{}" + count);
        System.out.println("执行完成 main");
    }

}
